const app = getApp();

Component({
    attached: function() {
        
    },
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        statusNum: '',
        myInfo: '', //个人信息
        studentList: [], //学生列表
    },

    /**
     * 组件的方法列表
     */
    methods: {
      init(callback) {
        this.get_user_info(); //获取个人信息
        this.get_student_list();
        callback && callback();
      },

        //去机构管理端
        to_school() {
            let that = this;
            wx.navigateToMiniProgram({
                appId: 'wx190f1d9f6c43c81e',
                path: 'pages/school/index'
            })
        },

        //去老师管理端
        to_teacher() {
            wx.navigateToMiniProgram({
                appId: 'wx190f1d9f6c43c81e',
                path: 'pages/teacher/index'
            })
        },
        //获取个人信息
        get_user_info: function() { 
            var that = this;
            app.request({
                url: 'user/index',
                sCallback: (res) => {
                    if (res.code == 1) {
                        that.setData({
                            myInfo: res.data
                        })
                    }
                }
            })
        },
        //添加学生
        add_student: function(e) { 
            wx.navigateTo({
                url: '/pages/addstudent/index',
            })
        },

        //我的订单
        to_order: function(e) { 
            console.log(e)
            wx.navigateTo({
                url: '/pages/myorder/index?status=' + e.currentTarget.dataset.status,
            })
        },

        //点击开始测试
        to_test: function(e) { 
            var year = 1000 * 60 * 60 * 24 * 365;
            var now = new Date();
            var birth = e.currentTarget.dataset.birthday;
            birth = Date.parse(birth.replace('/-/g', "/"));
            var birthday = new Date(birth);
            var age = parseInt((now - birthday) / year);
            var questionTypeId = 1
            if (age > 6) {
                questionTypeId = 2
            };
            wx.navigateTo({
                url: '/pages/test/index?studentId=' + e.currentTarget.dataset.id + '&questionTypeId=' + questionTypeId,
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
            })
        },
        get_student_list: function() { //获取学生列表
            var that = this;
            app.request({
              url: 'student/list',
              sCallback: (res) => {
                that.setData({
                  studentList: res.data
                })
              }
            })
        },
        edit_student: function(e) { //编辑学生
            wx.navigateTo({
                url: '/pages/editstudent/index?studentId=' + e.currentTarget.dataset.id + '&idx=' + e.currentTarget.dataset.idx,
            })
        },
        chat: function() {
            wx.navigateTo({
                url: '/pages/message/list/index'
            })
        }
    }
})